package com.xxx.common.model;


import com.xxx.common.constant.ParametersConstants;
import com.xxx.common.enums.DeviceType;
import com.xxx.common.enums.LoginType;
import com.xxx.servlet.HttpServletUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author revelin
 * @date 下午4:53 18-2-6
 **/
@ApiModel("id")
@Slf4j
public class ReqUserId extends ReqLanguageInfo{


	@ApiModelProperty(value="userId", hidden = true)
    @Setter
    private Long userId;


	/**
	 * 版本
	 */
	@ApiModelProperty(value = "版本", hidden = true)
	private  Long version;

	/**
	 * 设备类型 ios android
	 */
	@ApiModelProperty(value = "设备类型", hidden = true)
	private DeviceType deviceType;

	/**
	 * 终端
	 */
	@ApiModelProperty(value = "终端", hidden = true)
	private LoginType terminal;

	/**
	 * 设备编号
	 */
	@ApiModelProperty(value = "设备编号", hidden = true)
	private String deviceNumber;


	/**
	 *客户端ip
	 */
	@ApiModelProperty(value = "客户端ip", hidden = true)
	private String clientIp;

    public Long getUserId(){

		try {
			Object userIdObj = HttpServletUtil.getHttpServletRequest().getAttribute(ParametersConstants.USER_ID);
			if (userIdObj != null) {
				log.error("用户id:" + userIdObj.toString());
				return Long.parseLong(userIdObj.toString());
			} else {
				return this.userId;
			}
		}catch (Exception e){
			return null;
		}

	}

	public Long getVersion(){

		try {
			Object versionObj = HttpServletUtil.getHttpServletRequest().getAttribute(ParametersConstants.VERSION);
			if (versionObj != null) {
				log.error("版本:" + versionObj.toString());
				return Long.parseLong(versionObj.toString());
			} else {
				return this.version;
			}
		}catch (Exception e){
			return null;
		}

	}

	public DeviceType getDeviceType(){

		try {
			Object deviceTypeObj = HttpServletUtil.getHttpServletRequest().getAttribute(ParametersConstants.DEVICE_TYPE);
			if (deviceTypeObj != null) {
				log.error("版本:" + deviceTypeObj);
				return (DeviceType)deviceTypeObj;
			} else {
				return this.deviceType;
			}
		}catch (Exception e){
			return null;
		}

	}

	public LoginType getTerminal(){

		try {
			Object terminalObj = HttpServletUtil.getHttpServletRequest().getAttribute(ParametersConstants.TERMINAL);
			if (terminalObj != null) {
				log.error("终端:" + terminalObj);
				return (LoginType) terminalObj;
			} else {
				return LoginType.NAPP;
			}
		}catch (Exception e){
			return null;
		}

	}

	public String getDeviceNumber(){
		try {
			Object deviceNumberObj = HttpServletUtil.getHttpServletRequest().getAttribute(ParametersConstants.DEVICE_NUMBER);
			if (deviceNumberObj != null) {
				log.error("设备编号:" + deviceNumberObj);
				return deviceNumberObj.toString();
			} else {
				return this.deviceNumber;
			}
		}catch (Exception e){
			return null;
		}

	}


	public String getClientIp(){
		try {
			Object clientIpObj = HttpServletUtil.getHttpServletRequest().getAttribute(ParametersConstants.CLIENT_IP);
			if (clientIpObj != null) {
				log.error("客户端ip:" + clientIpObj);
				return  clientIpObj.toString();
			} else {
				return this.clientIp;
			}
		}catch (Exception e){
			return null;
		}
	}

	public void setVersion(Long version) {
		this.version = version;
	}

	public void setDeviceType(DeviceType deviceType) {
		this.deviceType = deviceType;
	}

	public void setTerminal(LoginType terminal) {
		this.terminal = terminal;
	}

	public void setDeviceNumber(String deviceNumber) {
		this.deviceNumber = deviceNumber;
	}

	public void setClientIp(String clientIp) {
		this.clientIp = clientIp;
	}

}
